/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.CabangDAOInterface;
import com.wii.elpro.dao.hb.CabangDAOImpl;
import com.wii.elpro.entity.Cabang;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.CabangUpdateForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class CabangUpdateAction extends Action {

    public CabangUpdateAction() {
    }

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;
        CabangUpdateForm cabangForm = (CabangUpdateForm) form;
        CabangDAOInterface cabangDao = new CabangDAOImpl();
        Cabang cabang = new Cabang();
        cabang.setId(cabangForm.getId());
        cabang.setAlamat(cabangForm.getAlamat());
        cabang.setEmail(cabangForm.getEmail());
        cabang.setKode(cabangForm.getKode());
        cabang.setKontak(cabangForm.getKontak());
        cabang.setKota(cabangForm.getKota());
        cabang.setTelp(cabangForm.getTelp());
        ActionMessages messages = new ActionMessages();       
        try {
            messages.add("customMessage", new ActionMessage("customMessage", "Your data is successfully updated."));
            saveMessages(sess, messages);
            cabangDao.update(cabang);
            forward = "success";
        } catch (Exception ex) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to update your data. Please contact my developer."));
            saveMessages(sess, messages);
            forward = "fail";
            ex.printStackTrace();
        }
        return mapping.findForward(forward);
    }
}
